import Vue from 'vue'
import Vuex from 'vuex'

import {
	setToken,
	getsToken,
	romoveToke
} from '@/utils/token.js'

import api from '@/api/index.js'
import webSocketModule from "./WebSocket.js"
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		token: getsToken(),
		// 登录后获取回来的用户信息
		UserInfo: {},
		imageObj: [],
		isLoading: false,
		msgCountobj: {},
		aipopIsShow: false,
		customShow:false
	},
	mutations: {
		GETTOKENDATE(state, value) {
			state.token = value
			//本地存储
			setToken(value)
		},
		GETUSERINFO(state, value) {
			state.UserInfo = value
		},
		BGIMAGE(state, value) {
			state.imageObj = value
		},
		LOGOUT(state, value) {
			state.UserInfo = {}
			state.imageObj = []
			state.token = null
			state.msgCountobj = {}
			sessionStorage.removeItem("store")
			romoveToke()
		},
		SET_LOADING(state, value) {
			state.isLoading = value
			if (value) {
				document.body.style.overflow = "hidden"
			} else {
				document.body.style.overflow = ""
			}
		},
		GETMSGCOUNTS(state, value) {
			state.msgCountobj = value
		},
		SET_ISAISHOW(state, value) {
			state.aipopIsShow = value
			state.customShow=false
		},
		STE_CUSTOMSHOW(state, value){
			state.customShow = value
			state.aipopIsShow=false
		}
	},
	actions: {
		async getUserInfo({
			commit, dispatch, state
		}) {
			let res = await api.user.userInfo()
			//获取动态的背景颜色
			if (res.code == 200) {
				commit("GETUSERINFO", res.data)
				// 建立连接
				if (!state.ws) {
					dispatch('startWebSocket', { "userId": res.data?.id });
				}
				let bgRes = await api.common.getBgImageList()
				if (bgRes.code == 200) {
					commit("BGIMAGE", bgRes.data)
				}
			} else {
				romoveToke()
			}
		},
		// 获取头部消息count
		async getMsgCount({
			commit
		}) {
			// this.$api.msg.getMsgCounts()
			let res = await api.msg.getMsgCounts()
			if (res.code == 200) {
				console.log("vuex" + res);
				commit("GETMSGCOUNTS", res.data)
			}
		},
		hide({ commit }) {
			commit("SET_LOADING", false)
		},
		show({ commit }) {
			commit("SET_LOADING", true)
		},
		aiShow({ commit }) {
			commit("SET_ISAISHOW", true)
		},
		aiHide({ commit }) {
			commit("SET_ISAISHOW", false)
		},
		customSHow({ commit }){
			commit("STE_CUSTOMSHOW", true)
		},
		customHide({ commit }){
			commit("STE_CUSTOMSHOW", false)
		}
	},
	modules: {
		webSocket: webSocketModule,
	},
	getters: {
		//获取首页背景
		getHomeBgColor(state) {
			let arr = state.imageObj
			if (arr != undefined && arr.length != 0) {
				return arr.find(item => {
					return item.bgModelName == "首页"
				})
			}
			return ""
		},
		// 获取归档背景
		getPigeonhole(state) {
			let arr = state.imageObj
			if (arr != undefined && arr.length != 0) {
				return arr.find(item => {
					return item.bgModelName == "归档"
				})
			}
			return ""
		},
		// 获取留言背景
		getBarrage(state) {
			let arr = state.imageObj
			if (arr != undefined && arr.length != 0) {
				return arr.find(item => {
					return item.bgModelName == "留言"
				})
			}
			return ""
		},
		//获取分类背景
		getCategory(state) {
			let arr = state.imageObj
			if (arr != undefined && arr.length != 0) {
				return arr.find(item => {
					return item.bgModelName == "分类"
				})
			}
			return ""
		},
		//获取我的文章背景
		getMyArticle(state) {
			let arr = state.imageObj
			if (arr != undefined && arr.length != 0) {
				return arr.find(item => {
					return item.bgModelName == "我的文章"
				})
			}
			return ""
		},
	}
})
